package com.gang.mars.aop.config;

import com.gang.mars.aop.proxy.springproxy.methodadvice.GangAfterReturningAdviceImpl;
import com.gang.mars.aop.proxy.springproxy.methodadvice.GangMethodBeforeAdviceImpl;
import com.gang.mars.aop.service.impl.HelloWorldServiceImpl;
import org.springframework.aop.aspectj.annotation.AnnotationAwareAspectJAutoProxyCreator;
import org.springframework.aop.framework.ProxyFactoryBean;
import org.springframework.aop.framework.autoproxy.BeanNameAutoProxyCreator;
import org.springframework.aop.framework.autoproxy.DefaultAdvisorAutoProxyCreator;
import org.springframework.aop.support.DefaultPointcutAdvisor;
import org.springframework.aop.support.NameMatchMethodPointcut;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Import;

/**
 * 类说明：gang.chen springboot深度实践
 *
 * @author gang.chen
 * @since 2022/5/15
 */
//@Import(DefaultAdvisorAutoProxyCreator.class)
//@Import(AnnotationAwareAspectJAutoProxyCreator.class)
@ComponentScan({"com.gang.mars.aop.proxy","com.gang.mars.aop.service","com.gang.mars.aop.config"})
public class AppConfig {


    public ProxyFactoryBean proxyFactoryBean(){
        HelloWorldServiceImpl originalService = new HelloWorldServiceImpl();
        ProxyFactoryBean proxyFactoryBean = new ProxyFactoryBean();
        proxyFactoryBean.addAdvice(new GangMethodBeforeAdviceImpl());
        proxyFactoryBean.setTarget(originalService);
        return proxyFactoryBean;
    }

    public BeanNameAutoProxyCreator beanNameAutoProxyCreator(){
        BeanNameAutoProxyCreator beanNameAutoProxyCreator = new BeanNameAutoProxyCreator();
        beanNameAutoProxyCreator.setBeanNames("HelloSe*");
        beanNameAutoProxyCreator.setInterceptorNames("GangMethodInterceptorImpl");
        beanNameAutoProxyCreator.setProxyTargetClass(true);
        return beanNameAutoProxyCreator;
    }

    @Bean
    public DefaultPointcutAdvisor defaultPointcutAdvisor(){
        NameMatchMethodPointcut pointcut = new NameMatchMethodPointcut();
        pointcut.addMethodName("test1");
        DefaultPointcutAdvisor defaultPointcutAdvisor = new DefaultPointcutAdvisor();
        defaultPointcutAdvisor.setPointcut(pointcut);
        defaultPointcutAdvisor.setAdvice(new GangAfterReturningAdviceImpl());
        return defaultPointcutAdvisor;
    }

    @Bean
    public DefaultAdvisorAutoProxyCreator defaultAdvisorAutoProxyCreator(){
        return new DefaultAdvisorAutoProxyCreator();
    }


}
